<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 *
 */
namespace osc\admin\controller;
use osc\common\controller\AdminBase;
use think\Db;
class Article extends AdminBase{
	
	protected function _initialize(){
		parent::_initialize();
		$this->assign('breadcrumb1','公告');
		$this->assign('breadcrumb2','公告列表');
	}
	
	//公告
	function index(){
		
		$param=input('param.');
		
		if(isset($param['id'])){
			$map['id']=$param['id'];
		}
		
		
		if(isset($param['status'])){
			$map['status']=$param['status'];
		}
		
		$map['show_type']='admin';
		
		
		$list=osc_service('common','Article')->get_article_by_cid($map);
		// print_r($list);exit;
		
		$this->assign('list',$list);
		
		$this->assign('empty', '~~暂无数据');
		
		return $this->fetch();  
	}

	//新增
	function add(){
		
		if(request()->isPost()){
			
			$data = $this->request->param();

			$files = request()->file('image');

            if(!$data || !$files){
                $this->error('没有内容，不能提交~');
            }	

			//标题内容验证
			$result=osc_model('admin','Article')->validate($data);
			if(isset($result['error'])){
				$this->error($result['error']);
			}


			$map = [
				'title'		=> $data['title'],
				'author'	=> admin('username'),
				'create_time' => time(),
				'status'	=> 0
			];


			$id = Db::name('Article')->insertgetid($map);

			if($id){
				//公告图片上传
	            $img = $this->doUpload($files,$id);
	            if($img['code'] == 1){
					storage_user_action(UID,admin('username'),config('BACKEND_USER'),'新增了公告-'.$data['title']);	
	                $this->success('新增成功','Article/index');
	            }else{
	                $this->error('上传失败');
	            }
			}else{
				$this->error('新增失败');
			}
					
		}
		$this->assign('crumbs','新增');
		return $this->fetch();  
	}


	//查看
	function edit(){
		$id = $this->request->param('id');

		if($id){
			$info = Db::name('article')->where('id',$id)->find();

			$img = Db::name('article_image')->where('article_id',$id)->select();

			$this->assign('info',$info);
			$this->assign('img',$img);

		}else{
			$this->error('请求错误');
		}
		$this->assign('crumbs','浏览');
		return $this->fetch(); 
	}
	
	//删除
	function del(){
		
		$id=(int)input('param.id');
		Db::name('Article')->where('id',$id)->delete();
		Db::name('article_data')->where('id',$id)->delete();
		
		storage_user_action(UID,admin('username'),config('BACKEND_USER'),'删除了公告-'.$id);	
		
		$this->redirect('Article/index');
	}

	//状态修改
	public function set_status(){
		$data=input('param.');
		
		Db::name('Article')->where('id',$data['id'])->update(['status'=>(int)$data['status']],false,true);		
		
		storage_user_action(UID,admin('username'),config('BACKEND_USER'),'修改了公告状态');	
		
		$this->redirect('Article/index');
	}

	//指定操作
	public function set_top(){
		$data=input('param.');
		
		Db::name('Article')->where('id',$data['id'])->update(['top'=>(int)$data['status']],false,true);		
		
		storage_user_action(UID,admin('username'),config('BACKEND_USER'),'修改了置顶状态');	
		
		$this->redirect('Article/index');
	}


	/**
     * 图片上传
     * @param $files    array       图片文件
     * @return int|string   array   返回值
     */
    function doUpload($files,$article_id){
        $info="";
        $map = null;
        foreach($files as $key=>$picFile){
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $picFile
                ->move(ROOT_PATH.'/public/uploads/images');
            /*获取存储路径，以便插入数据库*/
            $map[$key]['img_path']= "/public/uploads/images/".$info->getSaveName();
            $map[$key]['file_name'] = $info->getFilename();
            $map[$key]['article_id']  = $article_id;
            $map[$key]['create_time']  = time();
        }
        $img = Db::name('article_image')->insertAll($map);

        if($info!=="" && $img){
            return ['code'=>1,'msg'=>$img];
        }else{
            // 上传失败获取错误信息
            return ['code'=>0,'msg'=>$files->getError()];
        }
    }


}